package com.zhuoshi.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.zhuoshi.entity.Complex;
import com.zhuoshi.entity.Openingreport;
import com.zhuoshi.entity.PayMentTypeCustom;
import com.zhuoshi.entity.Project;
import com.zhuoshi.entity.Purpose;
import com.zhuoshi.entity.Room;
import com.zhuoshi.entity.RoomCustom;
import com.zhuoshi.entity.Type;
import com.zhuoshi.entity.Unit;
import com.zhuoshi.exception.CustomException;
import com.zhuoshi.service.ComplexService;
import com.zhuoshi.service.PayMentTypeService;
import com.zhuoshi.service.ProjectService;
import com.zhuoshi.service.PurposeService;
import com.zhuoshi.service.RoomService;
import com.zhuoshi.service.TypeService;
import com.zhuoshi.service.UnitService;

@Controller
@RequestMapping("room")
public class RoomController {
	
	@Autowired
	private RoomService roomService;
	
	@Autowired
	private TypeService typeService;
	
	@Autowired
	private PurposeService purposeService;
	
	@Autowired
	private UnitService unitService;
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private ComplexService complexService;
	
	@Autowired
	private PayMentTypeService payMentTypeService;
	
	//查看所有房间
	@RequestMapping("selectAllRoomLiu")
	public ModelAndView selectAllRoomLiu(String minArea,String maxArea) {
			Float minAreaf=null;
			Float maxAreaf=null;
			if(minArea!=null && minArea!="") {
				minAreaf=new Float(minArea);
			}
			if(maxArea!=null && minArea!="") {
				maxAreaf=new Float(maxArea);
			}
			List<RoomCustom> rooms=roomService.selectAllRoomLiu(minAreaf,maxAreaf);

		
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("rooms", rooms);
		modelAndndView.setViewName("/Admin/roomManager.jsp");
		return modelAndndView;
	}
	
	//查看所有房间:返回的都是未销售状态的(小伟)
	@RequestMapping("selectAllRoomYao")
	public ModelAndView selectAllRoomYao() throws Exception {
		List<RoomCustom> rooms=roomService.selectAllRoomYao();
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("rooms", rooms);
		modelAndndView.setViewName("/Admin/second/insertOpeningRooms.jsp");
		return modelAndndView;
	}
//	根据批量Id查房间
	@RequestMapping("selectRoomByIdsYao")
	public ModelAndView selectRoomByIdsYao(Integer[] ids) throws Exception {
		List<RoomCustom> roomCustoms=roomService.selectRoomByIdsYao(ids);
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("roomCustoms", roomCustoms);
		modelAndndView.setViewName("/Admin/second/selectRooms.jsp");
		return modelAndndView;
	}
	
//	根据批量Id查房间(但跳转的页面不同)
	@RequestMapping("daoruPriceYao")
	public ModelAndView daoruPriceYao(Integer[] ids) throws Exception {
		List<RoomCustom> roomCustoms=roomService.selectRoomByIdsYao(ids);
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("roomCustoms", roomCustoms);
		modelAndndView.setViewName("/Admin/second/daoRuPrice.jsp");
		return modelAndndView;
	}
	
//	根据批量Id查房间(导出Execl)
	@RequestMapping("exportExeclYao")
	public ModelAndView exportExeclYao(Integer[] ids) throws Exception {
		List<RoomCustom> roomCustoms=roomService.exportExeclYao(ids);
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("roomCustoms", roomCustoms);
		modelAndndView.setViewName("/Admin/second/daoRuPrice.jsp");
		return modelAndndView;
	}
	
//	读取Execl返回房间
	@RequestMapping("importExeclYao")
	public ModelAndView importExeclYao() throws Exception {
		List<RoomCustom> roomCustoms=roomService.importExeclYao();
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("roomCustoms", roomCustoms);
		modelAndndView.setViewName("/Admin/second/daoRuPrice.jsp");
		return modelAndndView;
	}
	
//	根据多个查询房间并接入付款方式
	@RequestMapping("selectRoomByIdsAndPayMentType")
	public ModelAndView selectRoomByIdsAndPayMentType(Integer[] ids,HttpSession session) throws Exception {
		List<RoomCustom> roomCustoms=roomService.importExeclYao();
		Openingreport openingreport=(Openingreport) session.getAttribute("openingreport");
		List<PayMentTypeCustom> payMentTypeCustoms=payMentTypeService.selectPayMentTypeByIds(ids);
		session.setAttribute("roomCustoms", roomCustoms);
		session.setAttribute("payMentTypeCustoms", payMentTypeCustoms);
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("roomCustoms", roomCustoms);
		modelAndndView.addObject("openingreport", openingreport);
		modelAndndView.addObject("payMentTypeCustoms",payMentTypeCustoms);
		modelAndndView.setViewName("/Admin/second/tiJiaoYulan.jsp");
		return modelAndndView;
	}

	//添加房间信息
	@RequestMapping("addRoomLiu")
	public ModelAndView addRoom() throws Exception {
		List<Type> types=typeService.selectAllTypeLiu();
		List<Project> projects=projectService.selectAllProjectLiu();
		List<Purpose> purposes=purposeService.selectAllPurposeLiu();
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("types", types);
		modelAndndView.addObject("projects", projects);
		modelAndndView.addObject("purposes", purposes);
		modelAndndView.setViewName("/Admin/addRoom.jsp");
		return modelAndndView;
	}
	
	//添加房间
	@RequestMapping("addRoomConfirmLiu")
	public String addRoomConfirmLiu(Room room) {
		Integer rows=roomService.addRoomLiu(room);
		if(rows>0) {
			return "redirect:selectAllRoomLiu.action";
		}else {
			return "redirect:error.jsp";
		}		
	}
	
	//删除房间
	@RequestMapping("deleteRoomLiu")
	public String deleteRoomLiu(String id) {
		String a []  = id.split(",");
		int b=0;
		for(int i=0;i<a.length;i++) {
			Integer ids=Integer.parseInt(a[i]);
			Integer rows=roomService.deleteRoomLiu(ids);
			b+=rows;
		}
		if(b==a.length){
			return "redirect:selectAllRoomLiu.action";
		}else {
			return "redirect:error.jsp";
		}	
	}
	
	//房间修改信息
	@RequestMapping("updateRoomMessageLiu")
	public ModelAndView updateRoomMessageLiu(Integer id) throws Exception {
		RoomCustom room=roomService.selectRoomCustomById(id);
		List<Type> types=typeService.selectAllTypeLiu();
		List<Project> projects=projectService.selectAllProjectLiu();
		List<Purpose> purposes=purposeService.selectAllPurposeLiu();
		ModelAndView modelAndndView=new ModelAndView();
		modelAndndView.addObject("room", room);
		modelAndndView.addObject("types", types);
		modelAndndView.addObject("projects", projects);
		modelAndndView.addObject("purposes", purposes);
		modelAndndView.setViewName("/Admin/updateRoom.jsp");
		return modelAndndView;
	}
	
	//确认修改
	@RequestMapping("updateRoomConfirmLiu")
	public String updateRoomConfirmLiu(Room room,Integer id) {
		int rows=roomService.updateRoomConfirmLiu(room,id);
		if(rows>0) {
			return "redirect:selectAllRoomLiu.action";
		}else {
			return "redirect:error.jsp";
		}
		
	}
}
